<?php
	require '../../include/library.inc.php';
	require '../../include/admin_login.inc.php';
		
	if (!isset($_POST['is_submitted'])) {
		$_POST['is_submitted'] = '';
	}
	if (!isset($error_message)) {
		$error_message = '';
	}	
	
	
	if($_POST["redirect"] || $_SESSION['redirect'] ){
		if($_POST["redirect"]) $redirect = $_POST["redirect"];
		if($_SESSION['redirect']){ 
			$redirect = $_SESSION['redirect'];
			unset($_SESSION['redirect']);
		}
	}else{
       $redirect = $_SERVER['HTTP_REFERER'];	
     }	
		
		if ($_POST['is_submitted']) {
			$error_message = '';
			$redirect = $_POST["redirect"];
		
	$sql = 'SELECT 
						lang_id,
						Name,
						code
						
					FROM
						language
					WHERE
						published
					
					';
	$result_lang = query($sql);
	
	while ($row_lang = mysql_fetch_object($result_lang)) {
	
	
				$em = $row_lang->lang_id;
				eval('$name =  $_POST[\'title_'.$em.'\'];');
				
				

	if (!$name)  $error_message .= 'Не е въведено заглавие на '.$row_lang->Name.'<br/>';
	}
	
	if (!$_POST['sef_url'])  $error_message .= 'Не е въведен class<br/>';
		elseif(!preg_match('/^[0-9A-Za-z\p{Cyrillic}\-\_]+$/u', $_POST['sef_url'])) $error_message .= 'Въведеният SEF URL е невалиден<br/>';
		
	$sql = "SELECT
				category_id
			FROM
				category
			WHERE
				sef_url = '".$_POST['sef_url']."'
			  ";
	$result = query($sql);
	if ($row_seo = mysql_fetch_object($result)) $error_message .= 'Този class вече е използван<br/>';
	
	
	
	//ОК
    if(!$error_message) {
		
	
		
		$sql = 'SELECT MAX(order_number)+1 AS order_number FROM category';
		$row_number = mysql_fetch_assoc(query($sql));
		
		//Създаваме проекта
		$sql = 'INSERT INTO category
				  (sef_url, published, order_number)
				VALUES
				  (\''.escape($_POST['sef_url']).'\', \''.intval($_POST['published']).'\', \''.$row_number['order_number'].'\')
				  ';
		query($sql);
		$id = mysql_insert_id();
		$sql = 'SELECT lang_id,Name,code
				FROM
				language
				WHERE
				published
					';
		$result_lang = query($sql);
		
		while ($row_lang = mysql_fetch_object($result_lang)) {
			
				$em = $row_lang->lang_id;
				eval('$name =  $_POST[\'title_'.$em.'\'];');
				eval('$description =  $_POST[\'description_'.$em.'\'];');
				eval('$meta_k =  $_POST[\'meta_key_'.$em.'\'];');
				eval('$meta_d =  $_POST[\'meta_description_'.$em.'\'];');
			
			$sql = "INSERT INTO category_description
				  (category_id, lang_id, Name, description, meta_key, meta_description)
				VALUES
				  ('".$id."','".$em."', '".escape($name)."', '".escape($description)."', '".escape($meta_k)."', '".escape($meta_d)."')";
				 
		query($sql);
		}
		
		
		$_SESSION['message'] .= 'Редакцията е записана успешно!';
         if($_POST["redirectForm"]){
           redirect($_POST["redirectForm"]);
		}else{
			$_SESSION['redirect'] = $redirect;
		   redirect('category_edit.php?category_id='.$id);
		}
    }

}
  	
	
	head_admin("Нова категория");
?>

<?php if ($error_message) { ?>

<div class="message red">
  <h3>Грешка!</h3>
  <?=$error_message?>
</div>
<?php } ?>

 <?php if ($_SESSION['message']) { ?>
			<div class="message green">
				
				<?=$_SESSION['message']?>
			</div>
			<?php 
			unset($_SESSION['message']);
			} ?>
            
<div class="topbuttons"> <a href="<?=$redirect?>">Обратно</a>
</div>
<h3>Създаване на категория</h3>
<form id="cat_new_form_id" action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
	<input type="hidden" name="is_submitted" value="1" />
	<input type="hidden" name="redirect" value="<?=$redirect?>" />
	<input id="redirectForm" type="hidden" name="redirectForm" value="" />
	<input  type="hidden" name="redirect" value="<?=$redirect?>" />	
 
	
    
    <!-- Tabs beginning-->
	<div id="tabs">
		<ul id="tabs-ui"></ul>
              	
 
  <?php 
			
			$sql = 'SELECT 
						lang_id,
						Name,
						code
						
					FROM
						language
					WHERE
						published
					
					';
			$result = query($sql);
			
			$i=0;
			while ($row_lang = mysql_fetch_object($result)) {
				
				
				$em = $row_lang->lang_id;
				$tabs.='<li><a href="'.$_SERVER['PHP_SELF'].'#tabs-'.$em.'">'.$row_lang->Name.'</a></li>';
				eval('$name =  $_POST[\'title_'.$em.'\'];');
				eval('$description =  $_POST[\'description_'.$em.'\'];');
				eval('$meta_k =  $_POST[\'meta_key_'.$em.'\'];');
				eval('$meta_d =  $_POST[\'meta_description_'.$em.'\'];');
				
								  
			?>
  			<div id="tabs-<?=$em?>">
  				<dl class="clearfix">
    				<h3>Данни на <?=$row_lang->Name?>
      				</h3>
    				<dt>
      					<label class="required">Заглавие на 
       					 <?=$row_lang->Name?>
     					 </label>
					</dt>
					<dd>
      				<input id="title_<?=$row_lang->lang_id?>" class="inputbox w500" name="title_<?=$row_lang->lang_id?>" type="text" value="<?=$name?>" >
    				</dd>
    				
                    
 
		</div>
		<?php
                    $i++;
                }
                
                ?>
	</div>
   
 		<dt><label class="required">Class:</label>
        <input class="inputbox w250" name="sef_url" type="text" value="<?=$_POST["sef_url"]?>" />
        </dt>
    
		<input class="checkbox" name="published" id="published" type="checkbox" value="1" <?=(($_POST['published'])?"checked=\"checked\"":"")?> />
    Published
   		</label>
    </dd>
   
	</dl>
</form>


<script>

	

	$(document).ready(function() {
		
		// Tabs
		$("#tabs-ui").html('<?=$tabs?>');
		$( "#tabs" ).tabs();	
		
	});
</script>
  
    
<?php
    controls($redirect, "#cat_new_form_id");
	foot_admin(); 
?>
